package com.cdtye.itps.cms.controller.rowdata;

import com.cdtye.common.authcore.model.UserSso;
import com.cdtye.common.authcore.util.AuthHelper;
import com.cdtye.common.webcore.base.BaseController;
import com.cdtye.common.webcore.constants.WebConstants;
import com.cdtye.common.webcore.util.DateUtil;
import com.cdtye.common.webcore.web.AjaxJson;
import com.cdtye.itps.cms.dao.rowdata.entity.JcjcRowDataImportLog;
import com.cdtye.itps.cms.dao.rowdata.vo.JcjcRowDataImportLogVo;
import com.cdtye.itps.cms.service.rowdata.JcjcRowDataImportLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 原始数据导入记录表 前端控制器
 * @author Jchen
 * @date 2021-09-24
 */
@Api(tags="原始数据导入记录表接口")
@RestController
@RequestMapping("/JcjcRowDataImportLog")
public class JcjcRowDataImportLogController extends BaseController {

    @Autowired
    private JcjcRowDataImportLogService modelService;

    private static Logger logger = LoggerFactory.getLogger(JcjcRowDataImportLogController.class);

    @ApiOperation(value = "保存或更新", notes = "返回对象最新数据")
    @PostMapping(value = "/saveOrUpdate")
    public AjaxJson<JcjcRowDataImportLog> saveOrUpdate(@Valid JcjcRowDataImportLog model){
        UserSso loginUser = AuthHelper.loginUser();
        Date nowDate = DateUtil.getDateTime();

        if(StringUtils.isBlank(model.getId())){
            model.setCreateBy(loginUser.getId());
            model.setCreateDatetime(nowDate);
            model.setUpdateDatetime(nowDate);
            //初始化删除状态
            model.setDelFlag(WebConstants.NO);
        }else{
            JcjcRowDataImportLog oldModel = modelService.getById(model.getId());
            if(oldModel==null){
                return error("未查询到修改对象");
            }
            bindRequestToModel(oldModel);
            model = oldModel;
        }
        model.setUpdateBy(loginUser.getId());
        model.setUpdateDatetime(nowDate);
        modelService.saveOrUpdate(model);
        return success(model);
    }

    @ApiOperation(value = "按ID删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "String")
    @PostMapping(value = "/{id}")
    public AjaxJson deleteById(@PathVariable("id") String id) {
        if(StringUtils.isBlank(id)){
            return error("未获取到参数");
        }
        modelService.removeById(id);
        return success();
    }

    @ApiOperation(value = "按ID状态删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "String")
    @PostMapping(value = "/deleteFlagById")
    public AjaxJson deleteFlagById(String id) throws Exception{
        if(StringUtils.isBlank(id)){
            return error("未获取到参数");
        }
        modelService.deleteFlagById(id);
        return success();
    }

    @ApiOperation(value = "按ID查询")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "String")
    @GetMapping(value = "/{id}")
    public AjaxJson getById(@PathVariable("id") String id) {
        if(StringUtils.isBlank(id)){
            return error("未获取到参数");
        }
        return success(modelService.getById(id));
    }

    @ApiOperation(value = "列表查询")
    @GetMapping(value = "/list")
    public AjaxJson<List<JcjcRowDataImportLog>> list(JcjcRowDataImportLogVo vo){
        List<JcjcRowDataImportLog> result = modelService.list(vo);
        return success(result);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/listPage")
    public AjaxJson<List<JcjcRowDataImportLog>> listPage(JcjcRowDataImportLogVo vo){
        List<JcjcRowDataImportLog> result = modelService.listPage(vo);
        return success(result);
    }

    @ApiOperation(value = "导入 数据(excel)")
    @PostMapping(value = "/upload")
    public AjaxJson<Object> upload(MultipartFile file) throws IOException {
        return success(modelService.importFile(file));
    }

    @ApiOperation(value = "数据模板下载")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response) {
        modelService.download(response);
    }

}
